﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.DALFactory;

namespace KPIS.GERP.GHRMS.BLL
{
    public class ContactBLL : IContactBLL
    {
        private static readonly IContactDAL dal = DataAccess.CreateContacts();

        private ContactInfo conInfo = new ContactInfo();
        private EmployeeInfo empInfo = new EmployeeInfo();
        private MasNamePrefixInfo prefixInfo = new MasNamePrefixInfo();
        private MasDistrictInfo disInfo = new MasDistrictInfo();
        private MasFamilyHoodInfo famInfo = new MasFamilyHoodInfo();
        private PersonInfo perInfo = new PersonInfo();

        public IList<ContactInfo> ListContacts(string Employee)
        {
            if (Employee.Length > 0)
            {
                empInfo.Sequence = Employee.Length > 0 ? Int32.Parse(Employee) : 0;
                conInfo.Employee = empInfo;
            }
            return dal.ListContacts(conInfo);
        }
        public IList<ContactInfo> ListContactBySequence(int Sequence)
        {
            return dal.ListContacts(new ContactInfo(Sequence));
        }
        public DataSet LoadContacts(Nullable<int> Sequence, Nullable<int> EmpSequence)
        {
            return dal.LoadContacts(Sequence, EmpSequence);
        }
        public void Insert(string Employee, string Prefixes, string Name, string LastName, string FamilyHood
            , string Career, string Address, string District, string PostalCode, string Tel, string Mobile
            , string Email, string Citizen, int Createdby)
        {
            empInfo.Sequence = Employee.Length > 0 ? Int32.Parse(Employee) : 0;
            disInfo.Sequence = District.Length > 0 ? Int32.Parse(District) : 0;
            famInfo.Sequence = FamilyHood.Length > 0 ? Int32.Parse(FamilyHood) : 0;
            prefixInfo.Sequence = Prefixes.Length > 0 ? Int32.Parse(Prefixes) : 0;
            perInfo.NamePrefix = prefixInfo;
            perInfo.FirstName = Name;
            perInfo.LastName = LastName;
            perInfo.Citizen = Citizen;
            perInfo.Career = Career;

            try
            {
                dal.Insert(new ContactInfo(0, empInfo, perInfo, famInfo, new AddressInfo(Address, PostalCode, Tel, "", Mobile, Email)
                    , disInfo, "", Createdby, 0));
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void Update(int Sequence, string Employee, string Prefixes, string Name, string LastName
            , string FamilyHood, string Career, string Address, string District, string PostalCode
            , string Tel, string Mobile, string Email, string Citizen, int UpdatedBy)
        {
            empInfo.Sequence = Employee.Length > 0 ? Int32.Parse(Employee) : 0;
            disInfo.Sequence = District.Length > 0 ? Int32.Parse(District) : 0;
            famInfo.Sequence = FamilyHood.Length > 0 ? Int32.Parse(FamilyHood) : 0;
            prefixInfo.Sequence = Prefixes.Length > 0 ? Int32.Parse(Prefixes) : 0;
            perInfo.NamePrefix = prefixInfo;
            perInfo.FirstName = Name;
            perInfo.LastName = LastName;
            perInfo.Citizen = Citizen;
            perInfo.Career = Career;

            try
            {
                dal.Update(new ContactInfo(Sequence, empInfo, perInfo, famInfo
                    , new AddressInfo(Address, PostalCode, Tel, "", Mobile, Email)
                    , disInfo, "", 0, UpdatedBy));
            }
            catch
            {
                throw new Exception("");
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            try
            {
                dal.Delete(Sequence, UpdatedBy);
            }
            catch
            {
                throw new Exception("");
            }
        }
    }
}
